package awesome.cpu.structure;

import awesome.cpu.utils.Mode;

public class ImmShifter extends NonSequentialComponent {
	
	private static boolean outputDebug = false;
	
	public final static int KEY_IMM9 = 1;
	private int mImm9;
	public final static int KEY_IMM5 = 2;
	private int mImm5;
	
	public final static int KEY_RESULT = 3;
	//private int mResult;
	
	private final static ImmShifter mInstance = new ImmShifter();
	
	private ImmShifter() {}
	
	public static ImmShifter getInstance() {
		return mInstance;
	}
	
	@Override
	boolean setData(int keyInterface, int data) {
		switch (keyInterface) {
		case KEY_IMM9:
			mImm9 = data;
			return true;
		case KEY_IMM5:
			mImm5 = data;
			return true;
		}
		return false;
	}

	@Override
	int getData(int keyInterface) {
		
		BusMonitor.transferData(InstructionRegister.getInstance(), 
				InstructionRegister.KEY_INSTRUCTION_13_9, 
				this, KEY_IMM5);
		BusMonitor.transferData(InstructionRegister.getInstance(), 
				InstructionRegister.KEY_INSTRUCTION_8_0, 
				this, KEY_IMM9);
		
		switch (keyInterface) {
		case KEY_RESULT:
			return result();
		}
		return 0;
	}
	
	private int result() {
		int r = mImm9 >>> mImm5 | mImm9 << (32-mImm5);
		if (outputDebug && Mode.getDebugLevel() >= Mode.DEBUG_LEVEL_PRINT_ALL){
			System.out.println("Imm Shifter data = " + mImm9);
			System.out.println("Imm Shifter count = " + mImm5);
			System.out.println("Imm Shifter = " + r);
		}
		return r;
	}
}